package cn.xspace.wnx.db;

import com.xspace.android.xafcommon.util.LogUtil;

import java.util.List;

import cn.xspace.wnx.data.IMessageData;
import cn.xspace.chatui.bean.MessageEntity;
import rx.Observable;
import rx.Subscriber;

/**
 * 会话列表的Dao
 * Created by liuchen on 2016/10/11.
 */
public class MessageRecordDao {

    private static MessageRecordDao instance;
    public static final String TABLE_NAME = "t_messagelist";
    public static final String COLUMN_TIME_STAMP = "inserttime";
    /**
     * 0：通知公告；1：微农信；2：单聊；3：群聊
     */
    public static final String COLUMN_MSG_TYPE = "msg_type";
    public static final String COLUMN_MSG_LOGO = "msg_logo";
    public static final String COLUMN_MSG_SENDER_ID = "msg_sender_id";
    public static final String COLUMN_MSG_SENDER_NAME = "msg_sender_name";
    public static final String COLUMN_MSG_RECEIVER_ID = "msg_receiver_id";
    public static final String COLUMN_MSG_RECEIVER_NAME = "msg_receiver_name";
    public static final String COLUMN_MSG_UNREAD_COUNT = "msg_unread_count";
    public static final String COLUMN_MSG_WARN_STATUS = "msg_warn_status";
    public static final String COLUMN_MSG_LAST_TIME = "msg_last_time";
    public static final String COLUMN_MSG_CONTENT_TYPE = "msg_content_type";
    public static final String COLUMN_MSG_CONTENT = "msg_content";
    public static final String COLUMN_MSG_SEND_STATUS = "msg_send_status";
    public static final String COLUMN_MSG_GROUP_ID = "msg_group_id";
    public static final String COLUMN_MSG_GROUP_NAME = "msg_group_name";
    public static final String COLUMN_MSG_GROUP_MEMBER = "msg_group_members";
    public static final String COLUMN_USER_SEX = "user_sex";
    public static final String COLUMN_MSG_INDEX = "msg_index";
    public static final String COLUMN_MSG_IS_SHOW = "msg_is_show";
    public static final String COLUMN_MSG_GROUP_TYPE = "msg_group_type";
    //    public static final String COLUMN_MSG_GROUP_MEMBER = "msg_group_member";
    public static final String COLUMN_REMARK1 = "remark1";
    public static final String COLUMN_REMARK2 = "remark2";
    public static final String COLUMN_REMARK3 = "remark3";

    public static MessageRecordDao getInstance() {
        if (instance == null) {
            instance = new MessageRecordDao();
        }
        return instance;
    }

    private MessageRecordDao() {
    }

    public static final String MSG_LIST_TABLE_CREATE = "CREATE TABLE  IF NOT EXISTS "
            + MessageRecordDao.TABLE_NAME
            + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
            + MessageRecordDao.COLUMN_TIME_STAMP + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_TYPE + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_LOGO + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_SENDER_ID + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_SENDER_NAME + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_RECEIVER_ID + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_RECEIVER_NAME + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_UNREAD_COUNT + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_WARN_STATUS + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_LAST_TIME + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_CONTENT_TYPE + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_CONTENT + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_SEND_STATUS + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_GROUP_ID + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_GROUP_NAME + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_GROUP_TYPE + " INTEGER,"
            + MessageRecordDao.COLUMN_MSG_GROUP_MEMBER + " INTEGER,"
            + MessageRecordDao.COLUMN_USER_SEX + " TEXT,"
            + MessageRecordDao.COLUMN_MSG_INDEX + " INTEGER,"
            + COLUMN_MSG_IS_SHOW + " INTEGER,"
            + MessageRecordDao.COLUMN_REMARK1 + " TEXT,"
            + MessageRecordDao.COLUMN_REMARK2 + " TEXT,"
            + MessageRecordDao.COLUMN_REMARK3 + " INTEGER)";

    public IMessageData getiMessageData() {
        return iMessageData;
    }

    IMessageData iMessageData = new IMessageData() {
        @Override
        public Observable<String> updateMessageList(final MessageEntity entity) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().saveOrUpdateMessageList(entity);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateMessageListReduce(final MessageEntity entity) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().insertOrUpdateNoticeReduce(entity);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> deleteSingleChat(final String sendId) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().deleteChatSingle(sendId);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> deleteGroupChat(final String groupId) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().deleteChatGroup(groupId);
                    subscriber.onNext("success");
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateUnreadStatus(final String id, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateUnreadStatus(id, chatType);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> topChatMessage(final String id, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().topChatMessage(id, chatType);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> cancelTopChatMessage(final String id, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().cancelTopChatMessage(id, chatType);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<List<MessageEntity>> getMessageList() {
            return Observable.create(new Observable.OnSubscribe<List<MessageEntity>>() {
                @Override
                public void call(Subscriber<? super List<MessageEntity>> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getMsgList());
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<List<MessageEntity>> getGroupList() {
            return Observable.create(new Observable.OnSubscribe<List<MessageEntity>>() {
                @Override
                public void call(Subscriber<? super List<MessageEntity>> subscriber) {
                    subscriber.onNext(WNXDBManager.getInstance().getGroupList());
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateMessageWarnStatus(final String id, final int chatType, final int status) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().warnStatusChangeMessage(id, chatType, status);
                    subscriber.onCompleted();
                }
            });
        }


//        @Override
//        public Observable<List<MessageEntity>> getNoticeList() {
//            return Observable.create(new Observable.OnSubscribe<List<MessageEntity>>() {
//                @Override
//                public void call(Subscriber<? super List<MessageEntity>> subscriber) {
//
//                    subscriber.onNext(WNXDBManager.getInstance().getNoticeList());
//                }
//            });
//        }
//
//        @Override
//        public Observable<List<MessageEntity>> getWNXList() {
//            return Observable.create(new Observable.OnSubscribe<List<MessageEntity>>() {
//                @Override
//                public void call(Subscriber<? super List<MessageEntity>> subscriber) {
//
//                    subscriber.onNext(WNXDBManager.getInstance().getWNXList());
//                }
//            });
//        }

        @Override
        public Observable<String> updateMessageEntityStatus(final String senderId, final int status, final int
                chatType) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateMessageEntityStatus(senderId, status, chatType);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateMessageLastTime(final String senderId, final String time, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateMessageEntityTime(senderId, time, chatType);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<MessageEntity> getMessageEntityByKeyId(final String senderId, final int chatType) {
            return Observable.create(new Observable.OnSubscribe<MessageEntity>() {
                @Override
                public void call(Subscriber<? super MessageEntity> subscriber) {
                    MessageEntity msgEntity = WNXDBManager.getInstance().getMessageEntity(senderId, chatType);
                    LogUtil.d("msgEntity>>" + (msgEntity == null));
                    subscriber.onNext(msgEntity);
                    subscriber.onCompleted();
                }
            });
        }

        @Override
        public Observable<String> updateGroupName(final String groupId, final String name) {
            return Observable.create(new Observable.OnSubscribe<String>() {
                @Override
                public void call(Subscriber<? super String> subscriber) {
                    WNXDBManager.getInstance().updateGroupName(groupId, name);
                    subscriber.onCompleted();

                }
            });
        }
    };

//    /**
//     * 保存微农信列表信息
//     * @param beanList
//     * @return
//     */
//    public Observable<String> saveWNXList(final ArrayList<WNXBean> beanList) {
//        return Observable.create(new Observable.OnSubscribe<String>() {
//            @Override
//            public void call(Subscriber<? super String> subscriber) {
//                try {
//                    WNXDBManager.getInstance().saveWNXList(beanList);
//                    subscriber.onNext("success");
//                    subscriber.onCompleted();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    subscriber.onError(e);
//                }
//
//            }
//        });
//    }


    /**
     * 更新消息列表中通知公告的内容
     *
     * @return
     */
    public MessageEntity updateMessageListNoticeContent() {
        return WNXDBManager.getInstance().updateMessageListNoticeContent();
    }

    /**
     * 更新消息列表中微农信的内容
     *
     * @return
     */
    public MessageEntity updateMessageListWNXContent() {
        return WNXDBManager.getInstance().updateMessageListWNXContent();
    }

    /**
     * 更新消息列表中群助手的内容
     *
     * @return
     */
    public MessageEntity updateMessageListGroupContent() {
        return WNXDBManager.getInstance().updateMessageListGroupContent();
    }

    /**
     * 插入或者更新列表
     */
    public void updateMessageList(MessageEntity entity) {
        WNXDBManager.getInstance().saveOrUpdateMessageList(entity);
    }

    public Observable<Integer> updateGroupType(final MessageEntity entity) {
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                subscriber.onNext(WNXDBManager.getInstance().updateGroupType(entity));
            }
        });
    }

    /**
     * 获取微农信列表
     * @return
     */
//    public Observable<MessageEntity> getWNXMessageList() {
//        return Observable.create(new Observable.OnSubscribe<MessageEntity>() {
//            @Override
//            public void call(Subscriber<? super MessageEntity> subscriber) {
//                subscriber.onNext(WNXDBManager.getInstance().getWNXMessageList());
//            }
//        });
//    }

//    /**
//     * 插入或者更新列表,已读减一的时候
//     */
//    public void updateMessageListReduce(MessageEntity entity) {
//        WNXDBManager.getInstance().insertOrUpdateNoticeReduce(entity);
//    }


//    /**
//     * 单聊更新打扰状态
//     */
//    public void updateSingleCharWarnStatus(String sendId, int status) {
//        WNXDBManager.getInstance().warnStatusChangeSingle(sendId, status);
//    }
//
//    /**
//     * 群聊更新打扰状态
//     */
//    public void updateGroupChatWarnStatus(String groupId, int status) {
//        WNXDBManager.getInstance().warnStatusChangeGroup(groupId, status);
//    }

    /**
     * 删除聊天，单聊
     */
    public void deleteSingleChat(String sendId) {
        WNXDBManager.getInstance().deleteChatSingle(sendId);
    }

    /**
     * 删除群聊
     */
    public void deleteGroupChat(String groupId) {
        WNXDBManager.getInstance().deleteChatGroup(groupId);
    }

    /**
     * 更新未读状态
     */
    public void updateUnreadStatus(String id, int chatType) {
        WNXDBManager.getInstance().updateUnreadStatus(id, chatType);
    }

    /**
     * 获取列表
     */
    public List<MessageEntity> getMessageList() {
        return WNXDBManager.getInstance().getMsgList();
    }


    /**
     * 修改某条单聊的信息发送状态
     *
     * @param senderId
     * @param status
     */
    public void updateMessageEntityStatus(String senderId, int status, int chatType) {
        WNXDBManager.getInstance().updateMessageEntityStatus(senderId, status, chatType);
    }

    /**
     * 根据snederid获取消息列表实体
     *
     * @param senderId
     * @return
     */
    public MessageEntity getMessageEntityBySenderId(String senderId, int chatType) {
        MessageEntity msgEntity = WNXDBManager.getInstance().getMessageEntity(senderId, chatType);
        return msgEntity;
    }

    /**
     * 清除聊天消息 的内容
     *
     * @param senderId
     * @param chatType
     * @return
     */
    public int clearMessageRecord(String senderId, int chatType) {
        int result = WNXDBManager.getInstance().clearMessageRecord(senderId, chatType);
        return result;
    }

//    /**
//     * 插入或者更新单聊
//     */
//    public void insertUpdate(MessageEntity messageEntity) {
//
//    }
//
//    /**
//     * 插入或者更新群聊
//     */
//    /**
//     * 插入或者更新微农信息或者公告
//     */

}
